import os

end_file = "xxx.txt"


def write_to_file(path, text):
    with open(path, 'a', encoding='utf-8') as f:
        f.writelines(text + '\n')


def write_to_file_and_clear(path, text):
    with open(path, 'w', encoding='utf-8') as f:
        f.writelines(text + '\n')


if __name__ == '__main__':
    img_path = input("路径： (/Users/victor/Program/Android/Gpower/Whatsapp/app/src/main/assets/1): ")
    tray_image_file = input("Ttray_image_file: ")

    g = os.walk(img_path)
    identifier = "1"
    rfind = img_path.rfind("/")
    if rfind != -1:
        identifier = img_path[rfind+1:]
    name = input("name: ")
    publisher = "LOLStickerStudio"
    publisher_email = ""
    publisher_website = ""
    privacy_policy_website = ""
    license_agreement_website = ""
    write_to_file_and_clear(end_file, "{")
    write_to_file(end_file, "\"identifier\": \"" + identifier + "\",")
    write_to_file(end_file, "\"name\": \"" + name + "\",")
    write_to_file(end_file, "\"publisher\": \"" + publisher + "\",")
    write_to_file(end_file, "\"tray_image_file\": \"" + tray_image_file + "\",")
    write_to_file(end_file, "\"publisher_email\": \"" + publisher_email + "\",")
    write_to_file(end_file, "\"publisher_website\": \"" + publisher_website + "\",")
    write_to_file(end_file, "\"privacy_policy_website\": \"" + privacy_policy_website + "\",")
    write_to_file(end_file, "\"license_agreement_website\": \"" + license_agreement_website + "\",")
    write_to_file(end_file, "\"stickers\": [")

    zero = True
    for path, d, filelist in g:
        for image_file in filelist:
            if image_file.endswith(".webp"):
                print(image_file)
                if zero:
                    write_to_file(end_file, "   {")
                else:
                    write_to_file(end_file, "   ,{")
                zero = False
                write_to_file(end_file, "       \"image_file\": \"" + image_file + "\",")
                write_to_file(end_file, "       \"emojis\": []")
                write_to_file(end_file, "   }")

    write_to_file(end_file, " ]")
    write_to_file(end_file, "}")
